package com.lock.subscribe.msg;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.lock.subscribe.constant.ClientDataEnum;
import com.lock.subscribe.constant.ClientOpenTypeEnum;
import com.lock.subscribe.util.Base64Util;
import com.lock.subscribe.util.ByteTools;
import com.lock.subscribe.util.DateUtil;
import com.lock.subscribe.util.HexUtils;

public class MessageConversionAdapter {
	private final static Integer sendTypeLength = 1;
	private final static Integer deviceId=4;
	private final static Integer deviceTypeLength =1;
	private final static Integer random=2;
	private final static Integer serviceType=2;
	private final static Integer timestamp=4;
	private final static Integer  count=2;
	private final static Integer  macLength=4;
	private final static Integer multiple=2;
	private final static Integer sendDataLength = 4;	
	private final static Integer  index=1;
	private final static Integer facesLength=4;
	private final static Integer offsizeLength = 4;
	private final static Integer codeIdLength = 4;
	private final static Integer maxGroupLength=3;
	private final static Integer minGroupLength =1;
	private final static String sendType01="01";// 小类型长度	
	private final static String sendType06="08";// 小类型长度	
	private final static String  inputService="03";
	private final static String  outputService="02";
	private final static String  startWith="68";
	private final static String  endWith="0016";
	public  final static Integer statusLength=1;// 接收数据小类型状态
	public  final static Integer setTimeLength=12;// 设置时间返回字段长度
	private final static Integer minitypeLength=1;// 小类型长度	
	private final static Integer reviceMiniCont=1;// 接收数据小类型
	private final static Integer pitNumLength=1;// 接收数据小类型坑位
	public static final String restring ="6977756e7530";//iwunu0
	public static final String nbInputService ="02";
	public static final String nbOutputService ="03";
	public static final String nbOnline="0000";
	public static final String nbOpenlock="0001";
	public static final String nbCmd ="0002";
	public static final String nbOpenLock="000101";
	public static final String nbMessageEnd = "0000";
	public static final String nbDelFingleType="20";
	public static final Integer fm226AllLength = 8;
	public  static Integer getBaseLength() {
		return (index + sendTypeLength + deviceTypeLength) * multiple;
	}

	public  static Integer getDeviceIdEnd() {
		return getBaseLength() + deviceId * multiple;
	}

	public  static Integer getRandomEnd() {
		return getDeviceIdEnd() + random * multiple;
	}

	public  static Integer getServiceTypeEnd() {
		return getRandomEnd() + serviceType * multiple;
	}

	public  static Integer getTimestampEnd() {
		return getServiceTypeEnd() + timestamp * multiple;
	}

	public static Integer getCountEnd() {
		return getTimestampEnd() + count * multiple;
	}

	public static String getDeviceId(String msg) {
		if (msg.startsWith(startWith) && msg.endsWith(endWith)) {
			return msg.substring(getBaseLength(), getDeviceIdEnd());
		}
		return null;
	}

	public static String getRandom(String msg) {
		return msg.substring(getDeviceIdEnd(), getRandomEnd());
	}

	public static String getServiceType(String msg) {
		return msg.substring(getRandomEnd(), getServiceTypeEnd());
	}

	public static String getTimestamp(String msg) {
		return msg.substring(getServiceTypeEnd(), getTimestampEnd());
	}

	public  static Integer getCount(String msg) {
		String length = msg.substring(getTimestampEnd(), getCountEnd());
		return Integer.parseInt(length,16);
	}
	//开始人脸锁返回数据(data)解析
	public static String getData(String msg) {
		Integer flaglength = statusLength* multiple;
		return msg.substring(getCountEnd(), getCountEnd() + getMiniCountEnd()+flaglength+pitNumLength*multiple);
	}
	public static String getCodeData(String msg) {
		Integer flaglength = statusLength* multiple;
		return msg.substring(getCountEnd(), getCountEnd() + getMiniCountEnd()+flaglength*multiple+codeIdLength*multiple);
	}
	public static Integer getCodeIdDelFlag(String msg) {
		String flag = msg.substring(minitypeLength*multiple+count,msg.length()- codeIdLength*multiple);
		return Integer.valueOf(flag);
	}
	public static String getCodeIdData(String msg) {
		return msg.substring(msg.length()-codeIdLength*multiple, msg.length());
	}
	//修改密码返回数据(data)解析
	public static String getUpdatePwdData(String msg) {
		Integer flaglength = statusLength* multiple;
		return msg.substring(getCountEnd(), getCountEnd() + getMiniCountEnd()+flaglength+pitNumLength*multiple+sendDataLength*multiple);
	}
	//获取密码(data)解析
	public static Integer getUpdatePwd(String data) {
		String pwd = data.substring(data.length()-sendDataLength*multiple,data.length());
		return  Integer.parseInt(pwd,16);
	}
	//上报日志数据返回
	public static String getReportData(String msg) {
		return msg.substring(getCountEnd(), getCountEnd() + getCount(msg)*multiple);
	}
	//获取小三元组类型
	public  static String getMiniType(String msg) {
		return msg.substring(0,minitypeLength* multiple);
	}
	//获取小三元组长度
	public  static String getMiniCount(String msg) {
		Integer start =minitypeLength * multiple;
		Integer end = reviceMiniCont* multiple;
		return msg.substring(start, end);
	}
	//获取小三元组长度结束位置
	public  static Integer getMiniCountEnd() {
		Integer start = minitypeLength * multiple;
		Integer end = reviceMiniCont* multiple;
		return start + end;
	}
	//获取小三元组成功标识00成功非00失败
	public  static Integer getSuccessFlag(String msg) {
		int start =getMiniCountEnd();
		Integer flaglength = statusLength* multiple;
		return Integer.parseInt(msg.substring(start, start+flaglength));
	}
	//获取小三元组坑位信息
	public  static Integer getpitNum(String msg) {
		int start =getMiniCountEnd();
		Integer flaglength =statusLength* multiple;
		Integer pitNumlengths = pitNumLength* multiple;
		return Integer.parseInt(msg.substring(start+flaglength, start+flaglength+pitNumlengths),16);
	}

	public  static String getFacesSize(String msg) {
		return msg.substring(getCountEnd(), getCountEnd() + getCount(msg));
	}

	public  static String getFacesIndex(String msg) {
		String data = msg.substring(getCountEnd(), getCountEnd() + getCount(msg)*2);
		return data.substring(data.length()-offsizeLength*multiple, data.length());
	}

	public String getMacData(String msg) {
		return msg.substring(getCountEnd(), getCountEnd() + getCount(msg));
	}

	/**
	 * 获取白名单
	 * 
	 * @param msg
	 * @return
	 */
	public List<String> getWhiteList(String msg) {
		List<String> whiteList = new ArrayList<String>();
		Integer length = getCount(msg);
		String macData = getMacData(msg);
		for (int i = 0; i < length / macLength; i++) {
			String mac = macData.substring(i * macLength, i * macLength + macLength);
			whiteList.add(mac);
		}
		return whiteList;
	}

	/**
	 ** 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendFacesInfo(String deviceId, Integer dataLength, String data, String serviceType,
			String miniType, String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(sendType01).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength + 3, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength,4)).append(data).append(endWith);
		return sendData.toString();
	}

	
	/**
	 ** 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendDataInfo(Integer pitNum, String startTime, String endTime, Integer size) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		sendData.append(HexUtils.toHex(size, 8));
		return sendData.toString();
	}
	/**
	 * 
	 * @param imageData
	 * @param index
	 * @return
	 */
	public static String spitImageDataRep(byte[] imageData,Integer index,Integer size,Integer pitNum) {//6977756e75303039 iwunu009
		String imageId = StringUtils.leftPad(pitNum.toString(),2, "0");
		String resHex =restring+HexUtils.stringToHexString(imageId);
		byte[] imageHex = Arrays.copyOfRange(imageData, index,  index+size);
		String result = HexUtils.bytesToHexString(imageHex, size);
		StringBuffer buf = new StringBuffer();
		buf.append(result);
		buf.replace(10, 10+resHex.length(), resHex);
		return buf.toString();
		
	}
	
	/**
	 ** 获取指纹下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendDataFeature(Integer pitNum,String data, String startTime, String endTime) {
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8)).append(data);
		return sendData.toString();
	}
	/**
	 ** 获取指纹下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendDataInfo(Integer pitNum,String data, String startTime, String endTime) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		sendData.append(data);
		return sendData.toString();
	}
	/**
	 ** 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendFm223Info(Integer pitNum,String data, String startTime, String endTime) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		sendData.append(HexUtils.toHex(data.length()/2, 8));
		sendData.append("02");
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendFm226FacesData(String deviceId, Integer dataLength, String data, String serviceType,
			String miniType, String random, String stamp,  Integer allCount, Integer count) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(sendType06).append(deviceId).append(random)
				.append(serviceType).append(stamp)
				.append(HexUtils.toHex((dataLength + maxGroupLength + 8 + facesLength+fm226AllLength)/2, 4)).append(miniType)
				.append(HexUtils.toHex((dataLength + offsizeLength + offsizeLength+fm226AllLength)/2, 4)).append(HexUtils.toHex(allCount/2, 8))
				.append(HexUtils.toHex(count, 8))
				.append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 ** 获取卡下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendDataCard(String data,Integer pitNum,String miniType, String startTime, String endTime) {
		StringBuilder send = new StringBuilder();
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2)).append(StringUtils.leftPad(data,8,"0"));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		send.append(miniType);
		send.append(HexUtils.toHexL(sendData.toString().length()/2+"",2)).append(sendData.toString());
		return send.toString();
	}
	/**
	 ** 获取密码下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendDataPwd(String data,Integer pitNum,String miniType, String startTime, String endTime) {
		String send = new String();
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2)).append(HexUtils.toHexL(data, 8));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		send=miniType+HexUtils.toHexL(sendData.toString().length()/2+"",2);
		return send+sendData.toString();
	}
	/**
	 ** 获取指纹下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendDataFinger(Integer pitNum,String data, String startTime, String endTime) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		sendData.append(data);
		return sendData.toString();
	}
	
	/**
	 ** 获取修改权限数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getUpdateData(Integer pitNum,String startTime, String endTime) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		startTime = DateUtil.dateToStampS(startTime) / 1000 + 60 * 60 * 8 + "";
		endTime = DateUtil.dateToStampS(endTime) / 1000 + 60 * 60 * 8 + "";
		sendData.append(HexUtils.toHexL(startTime, 8)).append(HexUtils.toHexL(endTime, 8));
		return sendData.toString();
	}
	/**
	 * 获取修改权限下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param type
	 * @param serviceType
	 * @param random
	 * @param stamp
	 * @param lockType
	 * @return
	 */
	public  static String getSendUpdateData(String deviceId, Integer dataLength, String data, String serviceType,String type,
			String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		String hexDataLength = HexUtils.toHex(dataLength , 4);
		Integer count = (type.length()+hexDataLength.length())/2+dataLength;
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(count , 4)).append(type).append(hexDataLength)
				.append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendGeneralData(String deviceId, Integer dataLength, String data, String serviceType, String miniType,
			String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength + minGroupLength+1, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength, 2)).append(data).append(endWith);
		return sendData.toString();
	}
	
	/**
	 * 获取冻结/解冻/催缴数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @param random
	 * @param stamp
	 * @param lockType
	 * @return
	 */
	public static String getSendSettingTimeData(String deviceId, Integer dataLength, String data, String serviceType,
			String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength, 4)).append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendGeneralData04(String deviceId, Integer dataLength, String data, String serviceType, String miniType,
			String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength + minGroupLength+2, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength, 4)).append(data).append(endWith);
		return sendData.toString();
	}
	
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendAddOpenTypeData01(String deviceId, Integer dataLength, String data, String serviceType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(sendType01).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength , 4))
				.append(data).append(endWith);
		return sendData.toString();
	}
	public static String getSendAddOpenTypeData(String deviceId, Integer dataLength, String data, String serviceType,
			String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength , 4))
				.append(data).append(endWith);
		return sendData.toString();
	}
	public  static String getSendAddFingerData(String deviceId, Integer dataLength, String data,String type, String serviceType,
			String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		String hexDataLength = HexUtils.toHex(dataLength , 4);
		Integer count = (type.length()+hexDataLength.length())/2+dataLength;
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(count , 4)).append(type).append(hexDataLength)
				.append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 * 获取下发删除数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendDelData01(String deviceId, Integer data, String serviceType, String miniType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(sendType01).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(3, 4)).append(miniType)
				.append(HexUtils.toHex(1, 2)).append(HexUtils.toHex(data, 2)).append(endWith);
		return sendData.toString();
	}
	public static String getSendDelData(String deviceId, Integer data, String serviceType, String miniType,
			String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(3, 4)).append(miniType)
				.append(HexUtils.toHex(1, 2)).append(HexUtils.toHex(data, 2)).append(endWith);
		return sendData.toString();
	}
	public static String getSendDelCodeData(String deviceId, String data, String serviceType, String miniType,
			String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(6, 4)).append(miniType)
				.append(HexUtils.toHex(4, 2)).append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendData06(String deviceId, Integer dataLength, String data, String serviceType, String miniType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(sendType06).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength +maxGroupLength, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength, 4)).append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendData(String deviceId, Integer dataLength, String data, String serviceType, String miniType,
			String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength +maxGroupLength, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength, 4)).append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public  static String getSendData01(String deviceId, Integer dataLength, String data, String serviceType, String miniType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(sendType01).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength +maxGroupLength, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength, 4)).append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendEmptyData(String deviceId, String data, String serviceType, String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(sendType06).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(data).append(endWith);
		return sendData.toString();
	}

	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getSendFacesData(String deviceId, Integer dataLength, String data, String serviceType,
			String miniType, String random, String stamp,String offsize,Integer count) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(sendType06).append(deviceId).append(random)
				.append(serviceType).append(stamp)
				.append(HexUtils.toHex(dataLength +maxGroupLength+offsizeLength+facesLength, 4)).append(miniType)
				.append(HexUtils.toHex(dataLength+offsizeLength+facesLength, 4)).append(HexUtils.toHex(count,8))
				.append(offsize)
				.append(data)
				.append(endWith);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */
	public static String getSendFacesData(String deviceId, Integer dataLength, String data, String serviceType,String type,
			String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		String hexDataLength = HexUtils.toHex(dataLength , 4);
		Integer count = (lockType.length()+hexDataLength.length())/2+dataLength;
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(count , 4)).append(type).append(hexDataLength)
				.append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 * 
	 * @param imageData
	 * @param index
	 * @return
	 */
	public static String spitFm226ImageData(String imageData, Integer index, Integer size) {
		String imageHex =imageData.substring(index, size);
		return imageHex;
	}
	/**
	 * 
	 * @return
	 */
	public static String getSendLength(Integer length) {
		return StringUtils.leftPad(length.toString(), 4);
	}

	/**
	 * 
	 * @param imageData
	 * @param index
	 * @return
	 */
	public static  String spitImageData(byte[] imageData,Integer index,Integer size) {
		if(imageData.length-index<size) {
			size = imageData.length-index;
		}
		byte[] imageHex = Arrays.copyOfRange(imageData, index,  index+size);
		return HexUtils.bytesToHexString(imageHex, size);
	}
	/**
	 * 开锁
	 * 
	 * @param status
	 * @param stamp
	 * @return
	 */
	public static String getOpenLockData(String status, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(status).append(stamp);
		return sendData.toString();

	}
	public  static String getOfflineData(String type, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(type).append(StringUtils.leftPad(stamp.length()/2+"", 2, "0")).append(stamp);
		return sendData.toString();

	}
	public  static String getOfflinePwd(String type, Integer data) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(type).append(StringUtils.leftPad(data.toString().length()/2+"", 2, "0")).append(HexUtils.toHex(data, 8));
		return sendData.toString();

	}
	/**
	 * 获取离线同步/删除指令
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @param miniType
	 * @param random
	 * @param stamp
	 * @return
	 */
	public  static String getOfflineSendData(String deviceId, String data,String groupLength,String minType,String random, String stamp,String lockType) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(lockType).append(deviceId).append(random)
				.append(minType).append(stamp).append(groupLength).append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 ** 更换IP跟端口
	 * 
	 * @param status
	 * @param stamp
	 * @return
	 */
	public static String getChangeIpPort(String ip, String port) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHexL(ip, 6)).append(HexUtils.toHex(Integer.valueOf(port), 2));
		return sendData.toString();

	}

	/**
	 ** 获取密码数据
	 * 
	 * @param status
	 * @param stamp
	 * @return
	 */
	public static String getPassworData(String ip, String port) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHexL(ip, 6)).append(HexUtils.toHex(Integer.valueOf(port), 2));
		return sendData.toString();

	}

	/**
	 * mac 地址字符串
	 * 
	 * @param msg
	 * @return
	 */
	public static String getMacString(String msg) {
		return msg.replace(",", "").replace(":", "");
	}
	
	/**
	 * *获取正确返回字符串
	 * 
	 * @param msg
	 * @return
	 */
	public static String getMsg(String msg) {
		String start =startWith+inputService;
		return msg.substring(msg.lastIndexOf(start), msg.length());
	}
	/**
	 * 获取下发开锁数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getNbOpenLockData(String random,String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(nbOutputService).append(random).append(nbOpenlock).append(stamp)
		.append(HexUtils.toHex(nbOpenLock.length()/2, 4)).append(nbOpenLock).append(nbMessageEnd);
		return sendData.toString();
	}
	/**
	 * 获取下发数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public static String getNbSendCmdData(String data,String random,String stamptime) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(nbOutputService).append(random).append(nbCmd).append(stamptime)
		.append(HexUtils.toHex(data.length()/2, 4)).append(data).append(nbMessageEnd);
		return sendData.toString();
	}
	/**
	 * 获取下发删除数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getNbSendDelCmdData(String data,String random,String stamptime) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(nbOutputService).append(random).append(nbCmd).append(stamptime)
		.append(HexUtils.toHex(data.length()/2, 4)).append(data);
		return sendData.toString();
	}
	/**
	 ** 获取下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 * 
	 */

	public static String getNbSendData(String data,Integer pitNum,String miniType, String startTime, String endTime) {
		StringBuilder send = new StringBuilder();
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		if(data.length()<8) {
			data = HexUtils.toHexL(data,4);
			sendData.append(StringUtils.leftPad(data,8,"0"));
		}else if(!miniType.equals(nbDelFingleType)) {
			sendData.append(data);
		}else {
			sendData.append("");
		}
		if(!startTime.equals("")) {
			startTime = DateUtil.dateToStampHex(startTime);
		}
		if(!endTime.equals("")) {
			endTime = DateUtil.dateToStampHex(endTime);
		}	
		sendData.append(startTime).append(endTime);
		send.append(miniType);
		send.append(HexUtils.toHexL(sendData.toString().length()/2+"",2)).append(sendData.toString());
		return send.toString();
	}

	/**
	 ** 获取下发数据
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 * 
	 */

	public static String getNbSendFingerData(String data,Integer pitNum,String miniType, String startTime, String endTime) {
		StringBuilder send = new StringBuilder();
		StringBuilder sendData = new StringBuilder();
		sendData.append(HexUtils.toHex(pitNum, 2));
		if(startTime!=null) {
			startTime = DateUtil.dateToStampHex(startTime);
			sendData.append(startTime);
		}
		if(endTime!=null) {
			endTime = DateUtil.dateToStampHex(endTime);
			sendData.append(endTime);
		}
		sendData.append(data);
		send.append(miniType);
		String length = HexUtils.toHexL(sendData.toString().length()/2+"",2);
		send.append(StringUtils.leftPad(length, 4, "0")).append(sendData.toString());
		return send.toString();
	}
	public  static String getCurrentTimeData(String type, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(type).append(StringUtils.leftPad(stamp.length()/2+"", 2, "0")).append(stamp);
		return sendData.toString();

	}
	/**
	 * 获取下发数据
	 * 人脸添加第一包数据
	 * 
	 * @param deviceId
	 * @param dataLength
	 * @param data
	 * @param serviceType
	 * @return
	 */

	public String getSendData0c(String deviceId, Integer dataLength, String data, String serviceType, String miniType,
			String random, String stamp) {
		StringBuilder sendData = new StringBuilder();
		sendData.append(startWith).append(outputService).append(sendType06).append(deviceId).append(random)
				.append(serviceType).append(stamp).append(HexUtils.toHex(dataLength + maxGroupLength, 4))
				.append(miniType).append(HexUtils.toHex(dataLength, 4)).append(data).append(endWith);
		return sendData.toString();
	}
	/**
	 * 设置催缴/冻结
	 * 
	 * @param status
	 * @param stamp
	 * @return
	 */
	public static String getSetTimeData(String flage,Integer length, String startTime,String overTime,String freezeTime) {
		String hexlength = HexUtils.toHexL(length.toString(),2);
		StringBuilder sendData = new StringBuilder();
		sendData.append(flage).append(hexlength).append(startTime).append(overTime).append(freezeTime);
		return sendData.toString();

	}
	/**
	 * 设置催缴/冻结返回数据
	 */
	public static String getSetTimeData(String msg) {
		return msg.substring(getCountEnd(), getCountEnd() + getMiniCountEnd()+setTimeLength*multiple);
	}
	public static boolean getResult(String msg) {
		if(msg.length()<setTimeLength*multiple) {
			return  false;
		}
		msg =  msg.substring(msg.length()-setTimeLength*multiple ,msg.length()).toUpperCase();
		boolean allContains = msg.replaceAll(String.valueOf("F"), "").isEmpty();
		boolean thawContains = msg.endsWith("FFFFFFFF");
		boolean overContains =  msg.endsWith("00000000");;
		return allContains||thawContains||overContains;
		
	}
	public static void main(String[] args) {
		System.out.println(getResult("570c68ea70f068ed13f0ffffffff"));
	}
}
